package 动态代理;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//测试自己是拦截
public class Test implements InvocationHandler{
	
	private PersonDo personDo;

	public void testSelfHandel() {
		this.personDo = new PersonDo();
		//生成personDo的代理人,并通过代理来执行自己的函数
		PersonInterface proxy = (PersonInterface)Proxy.newProxyInstance(personDo.getClass().getClassLoader(), personDo.getClass().getInterfaces(), this);
		proxy.say();
	}
	
	//拦截函数
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("代理人执行");
		//真正执行personDo处理
		Object result = method.invoke(personDo, args);
		return result;
	}
}
